//
//  UIButton+LayoutEdgeInsets.m
//  CustomDemo
//
//  Created by ZhaoJie on 2017/1/16.
//  Copyright © 2017年 ZhaoJie. All rights reserved.
//

#import "UIButton+LayoutEdgeInsets.h"

@implementation UIButton (LayoutEdgeInsets)

- (CGRect)layoutWithLayoutType:(CIButtonLayoutType)ALType withImageSize:(CGSize)imageSize withSpacing:(CGFloat)gap {
    UIButton* btn = self;
    if (imageSize.height == 0 || imageSize.width == 0) {
        imageSize = btn.imageView.frame.size;
    }
    
    CGSize titleSize = [[btn titleForState:UIControlStateNormal] boundingRectWithSize:CGSizeMake(NSUIntegerMax, NSUIntegerMax) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:btn.titleLabel.font?:[UIFont systemFontOfSize:14]} context:nil].size;
    
    switch (ALType) {
        case CIButtonLayoutTypeLeft:
        {
            CGRect rect  = {
                .origin = btn.frame.origin,
                .size = {
                    .width = imageSize.width + gap + titleSize.width,
                    .height = MAX(titleSize.height,imageSize.height),
                }
            };
            
            btn.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, gap);
            btn.titleEdgeInsets = UIEdgeInsetsMake(0, gap, 0, 0);
            
            return rect;
        }
            break;
        case CIButtonLayoutTypeRight:
        {
            CGRect rect  = {
                .origin = btn.frame.origin,
                .size = {
                    .width = imageSize.width + gap + titleSize.width,
                    .height = MAX(titleSize.height,imageSize.height),
                }
            };
            
            btn.imageEdgeInsets = UIEdgeInsetsMake(0, titleSize.width + gap, 0, 0);
            btn.titleEdgeInsets = UIEdgeInsetsMake(0, -imageSize.width, 0, imageSize.width + gap);
            
            return rect;
            
            break;
        }
        case CIButtonLayoutTypeTop:
        {
            CGRect rect  = {
                .origin = btn.frame.origin,
                .size = {
                    .width = MAX(titleSize.width,imageSize.width),
                    .height = imageSize.height + gap + titleSize.height,
                }
            };
            
            btn.imageEdgeInsets = UIEdgeInsetsMake(0, 0, titleSize.height + gap, -titleSize.width);
            btn.titleEdgeInsets = UIEdgeInsetsMake(imageSize.height + gap, -imageSize.width, 0, 0);
            
            return rect;
            break;
        }
        case CIButtonLayoutTypeBottom:
        {
            CGRect rect  = {
                .origin = btn.frame.origin,
                .size = {
                    .width = MAX(titleSize.width,imageSize.width),
                    .height = imageSize.height + gap + titleSize.height,
                }
            };
            
            btn.imageEdgeInsets = UIEdgeInsetsMake(titleSize.height + gap,0, 0, -titleSize.width);
            btn.titleEdgeInsets = UIEdgeInsetsMake(0, -imageSize.width, imageSize.height + gap, 0);
            
            return rect;
            
            break;
        }
        case CIButtonLayoutTypeCenter:
        {
            CGRect rect  = {
                .origin = btn.frame.origin,
                .size = {
                    .width = MAX(titleSize.width,imageSize.width),
                    .height = MAX(imageSize.height, titleSize.height)
                }
            };
            
            btn.imageEdgeInsets = UIEdgeInsetsZero;
            btn.titleEdgeInsets = UIEdgeInsetsMake(0, -CGRectGetWidth(rect), 0, 0);
            
            return rect;
            
            break;
        }
        default:
            break;
    }
}

- (void)layoutImageToTop:(CGFloat)spacing
{
    CGSize imageViewSize = self.imageView.frame.size;
    CGSize titleLabelSize = self.titleLabel.frame.size;
    CGSize textSize = [self.titleLabel.text sizeWithAttributes:@{NSFontAttributeName:self.titleLabel.font}];
    CGSize frameSize = CGSizeMake(ceilf(textSize.width), ceilf(textSize.height));
    if (titleLabelSize.width + 0.5 < frameSize.width) {
        titleLabelSize.width = frameSize.width;
    }
    CGFloat totalHeight = (imageViewSize.height + titleLabelSize.height + spacing);
    self.imageEdgeInsets = UIEdgeInsetsMake(- (totalHeight - imageViewSize.height), 0.0, 0.0, - titleLabelSize.width);
    self.titleEdgeInsets = UIEdgeInsetsMake(0, - imageViewSize.width, - (totalHeight - titleLabelSize.height), 0);
}

- (void)layoutImageToLeft:(CGFloat)spacing
{
    self.imageEdgeInsets = UIEdgeInsetsMake(0, -spacing, 0, 0);
    self.titleEdgeInsets = UIEdgeInsetsMake(0, 0, 0, -spacing);
}

- (void)layoutImageToBottom:(CGFloat)spacing
{
    CGSize imageViewSize = self.imageView.frame.size;
    CGSize titleLabelSize = self.titleLabel.frame.size;
    CGSize textSize = [self.titleLabel.text sizeWithAttributes:@{NSFontAttributeName:self.titleLabel.font}];
    CGSize frameSize = CGSizeMake(ceilf(textSize.width), ceilf(textSize.height));
    if (titleLabelSize.width + 0.5 < frameSize.width) {
        titleLabelSize.width = frameSize.width;
    }
    CGFloat totalHeight = (imageViewSize.height + titleLabelSize.height + spacing);
    self.imageEdgeInsets = UIEdgeInsetsMake(0.0, 0.0, - (totalHeight - imageViewSize.height), - titleLabelSize.width);
    self.titleEdgeInsets = UIEdgeInsetsMake(- (totalHeight - titleLabelSize.height), - imageViewSize.width, 0, 0);
}

- (void)layoutImageToRight:(CGFloat)spacing
{
    CGSize imageViewSize = self.imageView.frame.size;
    CGSize titleLabelSize = self.titleLabel.frame.size;
    CGSize textSize = [self.titleLabel.text sizeWithAttributes:@{NSFontAttributeName:self.titleLabel.font}];
    CGSize frameSize = CGSizeMake(ceilf(textSize.width), ceilf(textSize.height));
    if (titleLabelSize.width + 0.5 < frameSize.width) {
        titleLabelSize.width = frameSize.width;
    }
    self.imageEdgeInsets = UIEdgeInsetsMake(0.0, 0.0, 0, - (titleLabelSize.width * 2  + spacing));
    self.titleEdgeInsets = UIEdgeInsetsMake(0, - (imageViewSize.width * 2  +spacing), 0, 0);
}

@end
